﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AR.Entities;
using AR.ViewModels.Commands;
using System.Collections.ObjectModel;
using AR.Model;
using System.ServiceModel.DomainServices.Client;
using AR.Web;
using AR.CustomEntities;
using GalaSoft.MvvmLight.Command;
using System.Linq;
using AR.Web.Services;

namespace AR.ViewModels
{
    public class ResultatEntretienTechniqueViewModel : ViewModelBase
    {





        private string _note;
        public string Note
        {
            get
            {
                return _note;
            }
            set
            {
                if (value != _note)
                {
                    _note = value;
                    OnPropertyChanged("Note");
                }
            }
        }

        private ObservableCollection<QUESTIONS> _questions = new ObservableCollection<QUESTIONS>();
        public ObservableCollection<QUESTIONS> Questions
        {
            get
            {
                return _questions;
            }
            set
            {
                if (value != _questions)
                {
                    _questions = value;
                    OnPropertyChanged("Questions");
                }
            }
        }
        private List<REPONSE_TEST_TECHNIQUE_CANDIDAT> _checkedChoix = new List<REPONSE_TEST_TECHNIQUE_CANDIDAT>();
        public List<REPONSE_TEST_TECHNIQUE_CANDIDAT> CheckedChoix
        {
            get
            {
                return _checkedChoix;
            }
            set
            {
                if (value != _checkedChoix)
                {
                    _checkedChoix = value;
                    OnPropertyChanged("CheckedChoix");
                }
            }
        }
        private readonly EntretienTechniquetModel model;

        private List<THEME> _themes = new List<THEME>();
        public List<THEME> Themes
        {
            get
            {
                return _themes;
            }
            set
            {
                if (value != _themes)
                {
                    _themes = value;
                    OnPropertyChanged("Themes");
                }
            }
        }
        private ObservableCollection<REPONSE_TEST_TECHNIQUE_CANDIDAT> _reponses = new ObservableCollection<REPONSE_TEST_TECHNIQUE_CANDIDAT>();
        public ObservableCollection<REPONSE_TEST_TECHNIQUE_CANDIDAT> Reponses
        {
            get
            {
                return _reponses;
            }
            set
            {
                if (value != _reponses)
                {
                    _reponses = value;
                    OnPropertyChanged("Reponses");
                }
            }
        }
        public ResultatEntretienTechniqueViewModel(int idCANDIDAT)
        {
            model = new EntretienTechniquetModel();
            if (idCANDIDAT != 0)
            {
                EntretienTechniqueContext context = new EntretienTechniqueContext();
                context.Load<THEME>(context.GetTEST_TECHNIQUE_CANDIDAT2Query(idCANDIDAT)).Completed += (sender, arg) =>
                {
                    Themes = context.THEMEs.ToList();
                    context.Load<REPONSE_TEST_TECHNIQUE_CANDIDAT>(context.GetREPONSE_TEST_TECHNIQUE_CANDIDATByidQuery(idCANDIDAT)).Completed += (sender2, arg2) =>
                    {
                        var query = context.GetCANDIDATQuery().Where(_ => _.ID_CANDIDAT == idCANDIDAT);
                        context.Load<CANDIDAT>(query).Completed += (sender3, arg3) =>
                      {
                          CheckedChoix = context.REPONSE_TEST_TECHNIQUE_CANDIDATs.ToList();
                          CANDIDAT candidat= context.CANDIDATs.SingleOrDefault();
                          TimeSpan t = TimeSpan.FromSeconds(double.Parse(candidat.DUREE_TEST.ToString()));

                          string Duree = string.Format("{0:D2}h:{1:D2}m:{2:D2}s",
                                          t.Hours,
                                          t.Minutes,
                                          t.Seconds);
                          Note = "Note : " + candidat.NOTE_TEST.ToString() + "   Durée : " + Duree ;
                          this.GetAnswers();
                      };
                    };
                };
                Reponses.Clear();
                //this.Loadresultat(idCANDIDAT);


            }
        }




        private void GetAnswers()
        {
           
            foreach (var theme in Themes)
            {
                foreach (var question in theme.QUESTIONS)
                {
       
                    foreach (var choix in question.CHOIX)
                    {
                        choix.AnswerImageSource = "  ../../Assets/icons/Ok_Icon_16.png";

                        //si le choix et correct et le candidat
                        if (choix.IsCorrect)
                        {
                            if (CheckedChoix.Where(_ => _.CHOIX.IDCHOIX == choix.IDCHOIX).Any())
                            {
                                choix.IsChecked = true;
                               
                            }
                            else
                                choix.IsChecked = false;
                           // choix.AnswerImageSource = "  ../../Assets/icons/Delete_Icon_16.png";
                            //choix decoché +  incorrecy sign
                        }
                        else
                        {
                            choix.AnswerImageSource = "  ../../Assets/icons/Delete_Icon_16.png";
                            if (CheckedChoix.Where(_ => _.ID_CHOIX == choix.IDCHOIX).Any())
                            {
                                choix.IsChecked = true;
                              
                            }
                            else 
                            {
                                choix.IsChecked = false;
                             //   choix.AnswerImageSource = "  ../../Assets/icons/Ok_Icon_16.png";
                            }

                             //choix est coché+incorrect sign
                        }
                       
                    }
                  
                }
            }
           
        }

        public void Loadresultat(int idcandidat)
        {

            model.GetResultat(idcandidat).EntityAdded += OnEntityAdded;

        }

        private void OnEntityAdded(object sender,
                                 EntityCollectionChangedEventArgs<THEME> entityCollectionChangedEventArgs)
        {
            Themes.Add(entityCollectionChangedEventArgs.Entity);
        }
        private void OnEntityAdded(object sender,
                                EntityCollectionChangedEventArgs<REPONSE_TEST_TECHNIQUE_CANDIDAT> entityCollectionChangedEventArgs)
        {
            Reponses.Add(entityCollectionChangedEventArgs.Entity);
        }








    }
}
